package com.lanxigua.entity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Message;

import com.lanxigua.utils.ErrorException;

public class Sale extends Base<Sale> {
	public String id;
	public String shopname;
	public String title;
	public String desc;
	public String content;
	
	public Sale(){
		
	}
	
	public Sale(JSONObject json){
		super(json);
	}
	
	public Sale(JSONArray json){
		for (int i = 0; i < json.length(); i++) {
			try {
				this.list.add(new Sale(json.getJSONObject(i)));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}		
	}
	
	public Sale(Message message) throws ErrorException{
		if (message.what == 0) {
			try {
				if (message.obj.getClass() == Class
						.forName("org.json.JSONObject")) {
					Sale sale = new Sale((JSONObject) message.obj);
					this.id = sale.id;
					this.shopname = sale.shopname;
					this.title = sale.title;
					this.desc = sale.desc;
					this.content = sale.content;
				} else if (message.obj.getClass() == Class
						.forName("org.json.JSONArray")) {
					JSONArray array = (JSONArray) message.obj;
					for (int i = 0; i < array.length(); i++) {
						Sale sale = new Sale(
								(JSONObject) array.getJSONObject(i));
						this.list.add(sale);
					}
				}
			} catch (ClassNotFoundException e) {

			} catch (JSONException e) {

			}
		} else if (message.what == -1) {
			throw new ErrorException(message.obj.toString());
		}
	}
}